package com.zoe.onelink.actuate.autoconfigure;

import com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration;
import com.zoe.onelink.actuate.endpoint.OnelinkShutdownEndpoint;
import com.zoe.onelink.actuate.feign.FeignInfoContributor;
import com.zoe.onelink.actuate.info.OnelinkFrmInfoContributor;
import com.zoe.onelink.actuate.info.OnelinkEnvInfoContributor;
import com.zoe.onelink.actuate.property.OnelinkActuatorProperties;
import feign.Contract;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.actuate.autoconfigure.context.ShutdownEndpointAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint;
import org.springframework.boot.actuate.context.ShutdownEndpoint;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2022</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2022-04-20
 */
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(OnelinkActuatorProperties.class)
public class OnelinkActuatorAutoConfiguration {

    @Bean
    public InfoContributor onelinkInfoContributor(Environment environment) {
        return new OnelinkEnvInfoContributor(environment);
    }

    @Bean
    @ConditionalOnClass(name = "com.zoe.onelink.core.util.OnelinkFrameworkInfoUtil")
    public InfoContributor onelinkFrmInfoContributor() {
        return new OnelinkFrmInfoContributor();
    }

    @Configuration(proxyBeanMethods = false)
    @ConditionalOnClass(Contract.class)
    public static class FeignActuatorAutoConfiguration {

        @Bean
        @ConditionalOnProperty(prefix = OnelinkActuatorProperties.PREFIX, name = "feign.show-client-info", matchIfMissing = false)
        public InfoContributor feignInfoContributor(ApplicationContext applicationContext) {
            return new FeignInfoContributor(applicationContext);
        }

    }

    @Configuration(proxyBeanMethods = false)
    @ConditionalOnAvailableEndpoint(endpoint = ShutdownEndpoint.class)
    @ConditionalOnClass(NacosAutoServiceRegistration.class)
    @AutoConfigureBefore(ShutdownEndpointAutoConfiguration.class)
    @Slf4j
    public static class OnelinkActuatorEndpointAutoConfiguration {

        @Bean(destroyMethod = "")
        public ShutdownEndpoint onelinkShutdownEndpoint(OnelinkActuatorProperties actuatorProperties) {
            log.info("[OneLinkFrm] 注册优雅关机端点: [POST] /actuator/shutdown");
            return new OnelinkShutdownEndpoint(actuatorProperties.getGracefulShutdownNacosCloseWaitMillis());
        }

    }

}
